<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 18-10-23
 * Time: 下午2:46
 */

namespace app\activity\logic;


use extend\Functions;
use extend\STATUS_CODE;

class personalInformation
{
    /**
     * @purpose 获取个人信息
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function personalInformationGet (int $uid):array
    {
        $model  = new \app\model\Member();
        $result = $model
            ->where('uid',$uid)
            ->field('mobile,username,nickname,avatar,sex')
            ->find();
        if (!$result) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '获取失败'
            );
        }
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }

    /**
     * @purpose 修改个人信息
     * @param array $data
     * @param int $uid
     * @return array
     */
    public static function personalInformationEdit (array $data ,int $uid):array
    {
        if (!preg_match('/^1[3|4|5|6|7|8|9]\d{9}$/', $data['mobile']) ||
        empty($data['mobile'] || isset($data['mobile']))
        ) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '电话号码错误'
            );
        }
        if (empty($data['username']) || !isset($data['username'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '用户名错误'
            );
        }
        if (empty($data['nickname']) || !isset($data['nickname'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '昵称有误'
            );
        }
        if (empty($data['avatar']) || !isset($data['avatar'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '头像有误'
            );
        }
        if (empty($data['sex']) || !isset($data['sex'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '性别有误'
            );
        }
        $model  = new \app\model\Member();
        $result = $model
            ->where('uid',$uid)
            ->isUpdate(true)
            ->data($data)
            ->save();
        if (!$result) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '修改失败'
            );
        }
        return Functions::result(
            STATUS_CODE::SUCCESS,
            '修改成功'
        );
    }
}